// Decompiled by DJ v2.9.9.60 Copyright 2000 Atanas Neshkov Date: 2010-5-13
// 16:36:14
// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for
// new version!
// Decompiler options: packimports(3)
// Source File Name: AjaxIndexAction.java

package com.stnet.web.front;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.modules.commons.PageAction;
import com.opensymphony.xwork2.ActionContext;
import com.stnet.bases.Constants;
import com.stnet.entity.OnlineRecharge;
import com.stnet.entity.Standby;
import com.stnet.entity.Users;
import com.stnet.front.enterprice.Product;

public class OnlineRechargeAction extends PageAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int type;
	private int price;
	private String mobile;
	private int result;
	
	/**
	 * 异步查询价格参数
	 * */
	private String _price;
	private String _cztype;
	private String payMoney;
	
	//久久数字平台系统通知消息参数
	private String partner;
	private String out_trade_id;
	private String state;
	private String account;
	private int product_id;
	private int quantity;
	private String product_info;
	private String sign;
	private String charset;
	
	
	/**
	 * 省份
	 * */
	private String mobielZoneInfo;
	/**
	 *移动、电信或者联通之一 
	 */
	private String businessType;
	
	private List<Product> productList;
	
	private final String[] responseResult = new String[]{"成功","充值失败，请联系管理员!","余额不足,请先充值!"};
	
	private Map<String,String> responseState = new HashMap<String,String> ();	
	
	private String resultText;
	
	public OnlineRechargeAction() {
		mobile = "";
		type = 0;
		price = 0;
		result = 0;
		responseState.put("ok", "交易成功");
		responseState.put("doing", "充值中");
		responseState.put("fail", "失败");
		responseState.put("return", "已经退货");
	}
	
	
	/**
	 * 在线充值
	 * @return
	 * @throws Exception
	 */
	public String onlineRecharge() throws Exception {
		Users users = null;
		Double shikou = 0.00;
		Map session = (Map) ActionContext.getContext().getSession();
		final double tempPrice = getPayMoneyByPrice(mobile,String.valueOf(price),String.valueOf(type));
		/**
		 * 增加管理员设定的利润
		 * shikou = 99平台真正扣掉的价格 + 德诚设定的利润
		 * */
		shikou = this.getAgentPriceByPrice(price,tempPrice);
		if (session.get("member") != null) {
			users = (Users) session.get("member");
			if(users.getWalletbalance() < shikou){
				result = 2;
			}else{
				result = onlineRechargeManagerDao.onlineRecharge(mobile, price, type, users);
			}
		}else{
			result = 1;//充值失败
		}
		//case result == 0 充值成功，扣除会员的金额，update session跟数据库信息
		//case result !=0 返回错误信息，不做其它操作
		if(result == 0){
			resultText = responseResult[result];
			DecimalFormat df = new DecimalFormat("0.00"); 
			users.setWalletbalance(Double.parseDouble(df.format(users.getWalletbalance()-shikou)));
			usersManagerDao.updateUsers(users);
		}else if(result == 2){
			resultText = responseResult[2];
		}else{
			resultText = responseResult[1];
		}
		return Constants.SUCCESS_KEY;
	}
	
	/**
	 * 根据相应的价格,转换城实际扣用户的价格
	 * @param price
	 * @return
	 */
	public double getAgentPriceByPrice(int price,double _price){
		Double shikou = 0.00;
		
		DecimalFormat df = new DecimalFormat("0.00"); 
		
		Standby standby = standbyManagerDao.getFirstEntity();
		switch(price){
			case 10:{
				shikou = Double.parseDouble(df.format(_price + Double.parseDouble(standby.getStandby1())));
				break;
			}
			case 20:{
				shikou = Double.parseDouble(df.format(_price + Double.parseDouble(standby.getStandby2())));
				break;
			}
			case 30:{
				shikou = Double.parseDouble(df.format(_price + Double.parseDouble(standby.getStandby3())));
				break;
			}
			case 50:{
				shikou = Double.parseDouble(df.format(_price + Double.parseDouble(standby.getStandby4())));
				break;
			}
			case 100:{
				shikou = Double.parseDouble(df.format(_price + Double.parseDouble(standby.getStandby5())));
				break;
			}
			case 150:{
				shikou = Double.parseDouble(df.format(_price + Double.parseDouble(standby.getStandby6())));
				break;
			}
			case 200:{
				shikou = Double.parseDouble(df.format(_price + Double.parseDouble(standby.getStandby7())));
				break;
			}
			case 300:{
				shikou = Double.parseDouble(df.format(_price + Double.parseDouble(standby.getStandby8())));
				break;
			}
			case 500:{
				shikou = Double.parseDouble(df.format(_price + Double.parseDouble(standby.getStandby9())));
				break;
			}
			default:
				shikou += _price;
				break;
		}
		return shikou;
	}
	
	/**
	 * 保存充值记录
	 * @return
	 * @throws Exception
	 */
	public String addOnlineRecharge() throws Exception {
		OnlineRecharge onlineRecharge = onlineRechargeManagerDao.getByOuttradeid(out_trade_id);

		onlineRecharge.setState(responseState.get(state));
		onlineRecharge.setProductid(product_id);
		onlineRecharge.setQuantity(quantity);
		onlineRecharge.setPrice(((double)price)/100);
		onlineRecharge.setProductinfo(product_info);
		onlineRecharge.setEditetime(new Date());
		
		/**
		 * 这一句有很大的问题
		 * */
		onlineRecharge.setShikou(getAgentPriceByPrice(Integer.parseInt(onlineRecharge.getAmount().toString()),((double)price)/100));
		
		try {
			onlineRechargeManagerDao.updateOnlineRecharge(onlineRecharge);
			ServletActionContext.getResponse().setContentType("text/plain;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write("ok");
		} catch (RuntimeException e) {
			e.printStackTrace();
		}	
		return null;
	}
	
	public String getMobileZone() throws Exception{
		
		final double tempPrice = getPayMoneyByPrice(mobile,_price,_cztype);
		/**
		 * 增加管理员设定的利润
		 * shikou = 99平台真正扣掉的价格 + 德诚设定的利润
		 * */
		if(tempPrice != -1){
			double shikou = this.getAgentPriceByPrice(Integer.parseInt(_price),tempPrice);
			payMoney = String.valueOf(shikou);
		}else{
			payMoney = "-1";
		}
		return Constants.SUCCESS_KEY;
	}
	
	/**
	 * 取得99平台真正扣掉的价格 
	 * */
	public double getPayMoneyByPrice(String mobile,String price,String _cztype) throws Exception{
		
		payMoney = "-1";		
		String taobaoMobileApi = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel="+mobile;
		InputStream l_urlStream;
		String sCurrentLine = "";
		String sTotalString = "";
		URL url = new URL(taobaoMobileApi);
		URLConnection connection = url.openConnection();

		l_urlStream = connection.getInputStream();
		BufferedReader l_reader = new BufferedReader(new InputStreamReader(
				l_urlStream, "gbk"));
		while ((sCurrentLine = l_reader.readLine()) != null) {
			sTotalString += sCurrentLine;
		}
		int i = sTotalString.indexOf("province:'");
		int j = sTotalString.indexOf("中国");
		String province = sTotalString.substring(i+10,i+12);
		String businessType = sTotalString.substring(j+2,j+4);
				
		if("0".equals(_cztype) || "快充".equals(_cztype)){
			_cztype = "快充";
		}else{
			_cztype = "慢充";
		}
		
		StringBuffer sb = new StringBuffer(province).append(businessType).append(price).append("元").append(_cztype);
		String _sb = sb.toString();
		
		//查找所有充值信息
		//类型0 全部；1 手机快充(5 分钟到账);2 手机慢充(24 小时到账); 3 Q 币QQ 服务; 4 游戏账户直
		int type = 0;
		if(_cztype.equals("快充")){
			type = 1;
		}else if(_cztype.equals("慢充")){
			type = 2;
		}
		productList = this.productManagerDao.findProducts(type);
		for(Product product : productList){
			if(_sb.equals(product.getName())){
				payMoney = product.getPrice();
				break;
			}
		}
		return Double.parseDouble(payMoney);
		
	}
	

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public String getResultText() {
		return resultText;
	}

	public void setResultText(String resultText) {
		this.resultText = resultText;
	}

	public String[] getResponseResult() {
		return responseResult;
	}

	public String getPartner() {
		return partner;
	}

	public void setPartner(String partner) {
		this.partner = partner;
	}

	public String getOut_trade_id() {
		return out_trade_id;
	}

	public void setOut_trade_id(String out_trade_id) {
		this.out_trade_id = out_trade_id;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public int getProduct_id() {
		return product_id;
	}

	public void setProduct_id(int product_id) {
		this.product_id = product_id;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public String getProduct_info() {
		return product_info;
	}

	public void setProduct_info(String product_info) {
		this.product_info = product_info;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	
	public String getMobielZoneInfo() {
		return mobielZoneInfo;
	}

	public void setMobielZoneInfo(String mobielZoneInfo) {
		this.mobielZoneInfo = mobielZoneInfo;
	}


	public String getBusinessType() {
		return this.businessType;
	}


	public void setBusinessType(String businessType) {
		this.businessType = businessType;
	}


	public List<Product> getProductList() {
		return this.productList;
	}


	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}


	public String get_price() {
		return this._price;
	}


	public void set_price(String price) {
		this._price = price;
	}


	public String get_cztype() {
		return this._cztype;
	}


	public void set_cztype(String cztype) {
		this._cztype = cztype;
	}


	public void setPayMoney(String payMoney) {
		this.payMoney = payMoney;
	}


	public String getPayMoney() {
		return this.payMoney;
	}
	
	

}